package vn.edu.uit.hotelbooking.activity;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import vn.edu.uit.hotelbooking.R;
import vn.edu.uit.hotelbooking.utils.SimpleGestureFilter;
import vn.edu.uit.hotelbooking.utils.SimpleGestureFilter.SimpleGestureListener;
import vn.edu.uit.hotelbooking.utils.UserLocation;
import vn.edu.uit.hotelbooking.utils.UserLocation.LocationResult;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

/**
 * Home activity 
 * @author DQH 
 */
public class HomeActivity extends Activity implements SimpleGestureListener{
    /**
     * The slide menu 
     */
    //private SlideMenu slidemenu;    
    /** 
     * The Gesture detector 
     */
    private SimpleGestureFilter detectorGesture;    
    private ImageButton ibtnMenu;
    private UserLocation myLocation = new UserLocation();    
    private Location userLocation = null;
    private Button btnSearch;
    
    String address = ""; 
    
    /** The dialog */
    private ProgressDialog dialog;
    /** The handler to process  */
    /*Handler handler = new Handler(){

         (non-Javadoc)
         * @see android.os.Handler#handleMessage(android.os.Message)
         
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            dialog.dismiss();            
        }
    };*/
    
    @Override
    public void onCreate(Bundle savedInstanceState) {      
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_activity_layout);
        btnSearch = (Button)findViewById(R.id.btnSearch_Home);
        
        btnSearch.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent in = new Intent(HomeActivity.this, SearchActivity.class);
                startActivity(in);
                
                /*dialog = ProgressDialog.show(HomeActivity.this, "Loadding", "Get hotel...");
                new Thread(new Runnable() {
                    
                    @Override
                    public void run() {
                        // TODO Auto-generated method stub       
                        ArrayList<Hotel> arrcountry = HotelFetcher.getDistrict();
                        //Log.d("Arraylist hotel", arrcountry.toString());
                        handler.sendEmptyMessage(0);                
                    }
                }).start(); */               
            }
        });
        
        // Detect touched area
        detectorGesture = new SimpleGestureFilter(this,this); 
        
        /*slidemenu = (SlideMenu)findViewById(R.id.slideMenu);
        slidemenu.init(this, R.menu.slide, this, 333);
        ibtnMenu = (ImageButton) findViewById(R.id.ibtnMenu_Home);
        ibtnMenu.setOnClickListener(new OnClickListener() {     //when click on Menu button, show the slidemenu
            @Override
            public void onClick(View v) {
               slidemenu.show();               
            }
        });*/
        
        myLocation.getLocation(HomeActivity.this, locationResult);        
    }
    
    /**
     * The method to get the address from longtitude and latitude
     * @param latitude
     * @param longitude
     * @return the address of user
     */
    private String getAddress(double latitude, double longitude) {
        StringBuilder result = new StringBuilder();
        try {
            Geocoder geocoder = new Geocoder(this, Locale.getDefault());
            List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
            if (addresses.size() > 0) {
                Address address = addresses.get(0);
                result.append(address.getCountryName()).append("\n");   //viet nam
                result.append(address.getAdminArea()).append("\n");     //binh duong
                result.append(address.getLocality()).append("\n");      //di an
                result.append(address.getSubLocality()).append("\n");   //dong hoa 
                //result.append(address.getSubAdminArea()).append("\n");              
                //result.append(address.getThoroughfare()).append("\n");              
                
                /*int maxLines = address.getMaxAddressLineIndex();
                for (int i=0; i<maxLines; i++) {
                    String addressStr = address.getAddressLine(i);
                    result.append(addressStr);
                    result.append("\n");
                }*/
            }
        } catch (IOException e) {
            Log.e("tag", e.getMessage());
        }

        return result.toString();
    }
    
    /** The onclick slidemenu function
     * @param itemId the id of menu item
     */
    /*@Override
    public void onSlideMenuItemClick(int itemId) {
        // TODO Auto-generated method stub
         
    }*/

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        // TODO Auto-generated method stub
        this.detectorGesture.onTouchEvent(ev);
        return super.dispatchTouchEvent(ev);
    }

    
    /** The onSwipe function to process user actions (swipe right, swipe left...)
     * @param direction the direction 
     */
    @Override
    public void onSwipe(int direction) {
        // TODO Auto-generated method stub
        
       /* switch (direction) {

            case SimpleGestureFilter.SWIPE_RIGHT:       //user swipe from left to right
                if(slidemenu.MenuIsShown()) 
                    return;                             //if slidemenu was shown, return 
                else                                    //else show the slidemenu
                    slidemenu.show();
                break;
            case SimpleGestureFilter.SWIPE_LEFT:        //user swipe from right to left
                if(!slidemenu.MenuIsShown()) 
                    return;
                else
                    slidemenu.hide();
                break;
        }*/
    }

    @Override
    public void onDoubleTap() {
        // TODO Auto-generated method stub
        
    }
    

    /**
     * The function to get the location of user
     * @return user location (Location)
     */

    public LocationResult locationResult = new LocationResult() {
        @Override
        public void gotLocation(Location location) {
            // Got the location!
            userLocation = location;
            address = getAddress(userLocation.getLatitude(), userLocation.getLongitude());
            //Toast.makeText(getApplicationContext(), "Latitude: " + userLocation.getLatitude()+"\n Longitude: "+userLocation.getLongitude() , Toast.LENGTH_SHORT).show();
            Toast.makeText(HomeActivity.this, address, Toast.LENGTH_LONG).show();
        }
    };

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        myLocation.cancelTimer();
        super.onBackPressed();
    }      
    
    
}
